<?php

namespace app\beautiful\controller\admin\setting;

use app\beautiful\model\setting\RegionModel;

use app\BaseController;
use app\beautiful\model\setting\TableNumberModel;


class Region extends BaseController{

    public function list(){
        $limit=$this->request->limit;
        $page=$this->request->page;
        $parent_id = $this->request->param('parent_id',0);
        $shop_id=$this->request->shopId;
        $where=[];
        $order['sort']='desc';
        $order['id']='desc';
        $where['parent_id']=$parent_id;
        $where['shop_id']=$shop_id;
        $res_data=RegionModel::list($where,$order,$page,$limit);
        if(!empty($res_data['list'])){
            foreach ($res_data['list'] as &$list){
                $list['t_num']=TableNumberModel::where(['region_id'=>$list['id'],'is_delete'=>0])->count();
            }
        }
        return $this->success($res_data);
    }
    public function info(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('区域ID错误');
        }
        $where['id']=$id;
        $res_data=RegionModel::info($where);
        return $this->success($res_data);
    }
    public function del(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('区域ID错误');
        }
        $is_delete=1;
        $where['id']=$id;
        $info=RegionModel::info($where);
        if(empty($info)){
            return $this->error('数据不存在');
        }
        $res=RegionModel::up(['is_delete'=>$is_delete],$where);
        if(!$res){
            return $this->error('删除失败');
        }
        return $this->success();
    }
    public function effect(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('区域ID错误');
        }
        $is_effect=$this->request->param('is_effect',null);
        if($is_effect===null){
            return $this->error('is_effect参数错误');
        }
        $where['id']=$id;
        $info=RegionModel::info($where);
        if(empty($info)){
            return $this->error('数据不存在');
        }
        $res=RegionModel::up(['is_effect'=>$is_effect],$where);
        return $this->success();
    }
    public function sort(){
        $id = $this->request->param('id');
        $type = $this->request->param('type',null);
        if(empty($id)){
            return $this->error('区域ID错误');
        }
        if($type===null){
            return $this->error('type参数错误');
        }
        $where['id']=$id;
        $ShopBrandModel= new RegionModel();
        $info=$ShopBrandModel->where(['id'=>$id])->find();
        if(empty($info)) return $this->error('该区域不存在');
        $s=[];
        $s['parent_id']=$info['parent_id'];
        $s['shop_id']=$info['shop_id'];
        $s['is_delete']=0;
        $order['sort']='desc';
        $order['id']='desc';
        $list=$ShopBrandModel->where($s)->order($order)
            ->limit(10000)->page(1)
            ->select()
            ->toArray();
        $brand_id=array_column($list,'id');
        if($type==1){//前移
            $new_brand=$this->array_swap_forward($brand_id,$id);
        }elseif($type==2){//后移
            $new_brand=$this->array_swap_back($brand_id,$id);
        }
        $count=count($new_brand);
        foreach ($new_brand as $k=>$v){
            $sort=$count-$k;
            $s=[];
            $s['id']=$v;
            RegionModel::up(['sort'=>$sort],$s);
        }
        return $this->success();
    }
    //指定元素前移一位
    public function array_swap_forward($arr,$elem)
    {
        $ndx = array_search($elem,$arr) - 1;
        $b4 = array_slice($arr,0,$ndx);
        $mid = array_reverse(array_slice($arr,$ndx,2));
        $after = array_slice($arr,$ndx + 2);

        return array_merge($b4,$mid,$after);
    }
    //指定元素后移一位
    public function array_swap_back($arr,$elem)
    {
        $ndx = array_search($elem,$arr);
        $b4 = array_slice($arr,0,$ndx);
        $mid = array_reverse(array_slice($arr,$ndx,2));
        $after = array_slice($arr,$ndx + 2);

        return array_merge($b4,$mid,$after);
    }

    public function edit(){
        $id = $this->request->param('id',0);
        $parent_id = $this->request->param('parent_id',null);
        $name = $this->request->param('name',null);
        if($id==0){
            return $this->error('区域ID错误');
        }
        $ShopBrandModel= new RegionModel();
        $info=$ShopBrandModel->where(['id'=>$id])->find();
        if(empty($info)) return $this->error('该区域不存在');
        if($parent_id!==null && $parent_id!=0){
            $p_info=$ShopBrandModel->where(['id'=>$parent_id])->find();
            if(empty($p_info)) return $this->error('父级区域不存在');
        }
        $up=[];
        if($parent_id!=null) $up['parent_id']=$parent_id;
        if($name!=null) $up['name']=$name;
        $where['id']=$id;
        $res_data=RegionModel::up($up,$where);
        return $this->success($res_data);
    }
    public function add(){
        $parent_id = $this->request->param('parent_id',0);
        $is_effect = $this->request->param('is_effect',null);
        $name = $this->request->param('name','');
        if($name=='') return $this->error('区域名称不能为空');
        if($is_effect===null) return $this->error('is_effect参数错误');
        $shop_id=$this->request->shopId;
        $ShopBrandModel= new RegionModel();
        $s=[];
        $s[]=['name','=',$name];
        $s[]=['is_delete','=',0];
        $s[]=['parent_id','=',$parent_id];
        $s[]=['shop_id','=',$shop_id];
        $info=$ShopBrandModel->where($s)->find();
        if(!empty($info)) return $this->error('该区域已存在');
        $s=[];
        $s[]=['is_delete','=',0];
        $s[]=['parent_id','=',$parent_id];
        $s[]=['shop_id','=',$shop_id];
        $sinfo=$ShopBrandModel->where($s)->order(['sort'=>'desc'])->find();
        if(empty($sinfo)){
            $sort=1;
        }else{
            $sort=$sinfo['sort']+1;
        }
        $insert=compact('parent_id','name','is_effect','sort','shop_id');
        $insert['add_time']=date('Y-m-d H:i:s');
        $insert['edit_time']=date('Y-m-d H:i:s');
        $insert_id=RegionModel::insert($insert);
        return $this->success($insert_id);
    }
    public function formatBrand(array $data, string $idName = "id", string $fieldName = 'pid', $childrenKey = 'children')
    {
        $items = [];
        foreach ($data as $item) {
            $items[$item[$idName]] = $item;
        }
        $result = array();
        $count=count($items);
        foreach ($items as $item) {
            if($count>1){
                if (isset($items[$item[$fieldName]])) {
                    $items[$item[$fieldName]][$childrenKey][] = &$items[$item[$idName]];
                } else if($item[$fieldName] == 0){
                    $result[] = &$items[$item[$idName]];
                }
            }else{
                $result[] = $item;
            }
        }
        return $result;
    }

}